package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        // 将DTO中的数据拷贝到setmeal中
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.add(setmeal);
        System.out.println(setmeal.getId());
        /*
         * 因为DTO中包含了套餐信息和菜品信息
         * 所以要分别处理
         * 以下是对菜品信息进行添加
         */
        // 菜品信息集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 如果集合有信息 给每一个设置套餐ID
        if (setmealDishes != null) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 数据层条件查询
        Page<SetmealVO> p = setmealMapper.page(setmealPageQueryDTO);
        // 封装对象返回表现层
        PageResult pr = new PageResult();
        pr.setTotal(p.getTotal());
        pr.setRecords(p.getResult());
        return pr;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 回显套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        // 查询两类信息 dish  setmeal
        Setmeal setmeal = setmealMapper.getById(id);
        // 查询SetmealDish
        List<SetmealDish> list = setmealDishMapper.getByDishId(id);
        // 分装到VO
        SetmealVO setmealVO = new SetmealVO();
        // 拷贝到VO
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        // setmealVO.setSetmealDishes();
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        // 将DTO拷贝到Setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 实现修改套餐的功能
        setmealMapper.updateSetmeal(setmeal);
        Long id = setmealDTO.getId();
        // 实现删除菜品的功能
        setmealDishMapper.deleteDish(id);
        List<SetmealDish> dishList = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : dishList) {
            setmealDish.setSetmealId(id);
        }
        // 新增关联关系
        setmealDishMapper.insertBatch(dishList);
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealDishMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                // 起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            setmealDishMapper.deleteDish(id);
            setmealMapper.deleteById(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }


    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
